package com.starwish.starwishblog.web.front;

import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.CommentService;
import com.starwish.starwishblog.vo.CommentVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("comment")
@Api
public class FCommentController {

    @Autowired
    CommentService commentService;

    @GetMapping("/getCommentsByAid")
    public ResultResponse<PageInfo<CommentVo>> getCommentsBYAid(@RequestParam(value="now",defaultValue = "1")Integer now,
                                                           @RequestParam(value="size",defaultValue = "3")Integer size,
                                                           Integer aid){
        ResultResponse<PageInfo<CommentVo>> result = null;

        try {
            PageInfo<CommentVo> commentsByArticleId = commentService.findCommentsByArticleId(now, size, aid);
            result = new ResultResponse<PageInfo<CommentVo>>(200,"查询评论成功",commentsByArticleId);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<PageInfo<CommentVo>>(305,"查询评论失败");
        }
        return result;
    }

    @PostMapping("addComment")
    public ResultResponse<Void> addComment(@RequestBody CommentVo commentVo){
        ResultResponse<Void> result = null;
        commentVo.setIsDelete(0);
        try {
            int i = commentService.addCommentFront(commentVo);
            if(i > 0){
                result = new ResultResponse<>(200,"评论成功");
            }else{
                result = new ResultResponse<>(201,"评论失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"评论异常");
        }
        return result;
    }
}
